package com.gjy.sql.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-03-05 16:17:04
 */
public class FileUtil {

    public static List<String> readSqlFile(String fileName) throws IOException {
        // Read the SQL file and return a list of SQL statements
        String s = FileUtils.readFileToString(Paths.get(fileName).toFile(), StandardCharsets.UTF_8);
        return Arrays.stream(s.split(";"))
                .filter(StringUtils::isNotBlank)
                .map(e -> {
                    String trim = e.trim();
                    trim = trim.replaceAll("\\s+", " ");
                    return trim;
                })
                .collect(Collectors.toList());
    }

}
